{
 "cells": [
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:13:38.871802600Z",
     "start_time": "2024-09-18T11:13:38.633641200Z"
    }
   },
   "id": "955def2dff9332a5",
   "execution_count": 25
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:13:39.038910200Z",
     "start_time": "2024-09-18T11:13:38.690679Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  age_range  gender\n0   376517        6.0     1.0\n1   234512        5.0     0.0\n2   344532        5.0     0.0\n3   186135        5.0     0.0\n4    30230        5.0     0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>376517</td>\n      <td>6.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>234512</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>344532</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>186135</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>30230</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info = pd.read_csv('user_info_format1.csv')\n",
    "user_info.head()"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  item_id  cat_id  seller_id  brand_id  time_stamp  action_type\n0   328862   323294     833       2882    2661.0         829            0\n1   328862   844400    1271       2882    2661.0         829            0\n2   328862   575153    1271       2882    2661.0         829            0\n3   328862   996875    1271       2882    2661.0         829            0\n4   328862  1086186    1271       1253    1049.0         829            0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>item_id</th>\n      <th>cat_id</th>\n      <th>seller_id</th>\n      <th>brand_id</th>\n      <th>time_stamp</th>\n      <th>action_type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>328862</td>\n      <td>323294</td>\n      <td>833</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>328862</td>\n      <td>844400</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>328862</td>\n      <td>575153</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>328862</td>\n      <td>996875</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>328862</td>\n      <td>1086186</td>\n      <td>1271</td>\n      <td>1253</td>\n      <td>1049.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log = pd.read_csv('user_log_format1.csv')\n",
    "user_log.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:16:47.136118200Z",
     "start_time": "2024-09-18T11:13:39.037909900Z"
    }
   },
   "id": "2c525cb895b58ac5",
   "execution_count": 27
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id         0\nage_range    2217\ngender       6436\ndtype: int64"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:16:48.275872700Z",
     "start_time": "2024-09-18T11:16:03.601332600Z"
    }
   },
   "id": "2e9d2aa998b4b3b9",
   "execution_count": 28
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id            0\nitem_id            0\ncat_id             0\nseller_id          0\nbrand_id       91015\ntime_stamp         0\naction_type        0\ndtype: int64"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:16:48.278875900Z",
     "start_time": "2024-09-18T11:16:04.374846400Z"
    }
   },
   "id": "b52a126a0523a131",
   "execution_count": 29
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 424170 entries, 0 to 424169\n",
      "Data columns (total 3 columns):\n",
      " #   Column     Non-Null Count   Dtype  \n",
      "---  ------     --------------   -----  \n",
      " 0   user_id    424170 non-null  int64  \n",
      " 1   age_range  421953 non-null  float64\n",
      " 2   gender     417734 non-null  float64\n",
      "dtypes: float64(2), int64(1)\n",
      "memory usage: 9.7 MB\n"
     ]
    }
   ],
   "source": [
    "user_info.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:16:48.318900400Z",
     "start_time": "2024-09-18T11:16:05.953888100Z"
    }
   },
   "id": "7dffd89587d5fc0f",
   "execution_count": 30
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 54925330 entries, 0 to 54925329\n",
      "Data columns (total 7 columns):\n",
      " #   Column       Dtype  \n",
      "---  ------       -----  \n",
      " 0   user_id      int64  \n",
      " 1   item_id      int64  \n",
      " 2   cat_id       int64  \n",
      " 3   seller_id    int64  \n",
      " 4   brand_id     float64\n",
      " 5   time_stamp   int64  \n",
      " 6   action_type  int64  \n",
      "dtypes: float64(1), int64(6)\n",
      "memory usage: 2.9 GB\n"
     ]
    }
   ],
   "source": [
    "user_log.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:16:48.321904Z",
     "start_time": "2024-09-18T11:16:07.146679400Z"
    }
   },
   "id": "a57b928566bc0c2e",
   "execution_count": 31
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(54925330, 7)"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:16:48.345919500Z",
     "start_time": "2024-09-18T11:16:07.179700100Z"
    }
   },
   "id": "78ef6fdac1a8b2ed",
   "execution_count": 32
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_2776\\2374263824.py:2: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_info['age_range'].replace(np.nan,2,inplace=True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_2776\\2374263824.py:3: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_info['gender'].replace(np.nan,-1,inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": "user_id      0\nage_range    0\ngender       0\ndtype: int64"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去除空值\n",
    "user_info['age_range'].replace(np.nan,2,inplace=True) # 2和NULL表示未知\n",
    "user_info['gender'].replace(np.nan,-1,inplace=True)\n",
    "user_info.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:16:48.348920700Z",
     "start_time": "2024-09-18T11:16:07.220727300Z"
    }
   },
   "id": "fb4b8f869391b193",
   "execution_count": 33
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_2776\\1715364757.py:1: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_log['brand_id'].replace(np.nan,-1,inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": "user_id        0\nitem_id        0\ncat_id         0\nseller_id      0\nbrand_id       0\ntime_stamp     0\naction_type    0\ndtype: int64"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log['brand_id'].replace(np.nan,-1,inplace=True)\n",
    "user_log.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:16:48.374939200Z",
     "start_time": "2024-09-18T11:16:07.295777Z"
    }
   },
   "id": "26c9ebd5dccdc3c3",
   "execution_count": 34
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "13750198\n"
     ]
    }
   ],
   "source": [
    "print(user_info.duplicated().sum())\n",
    "print(user_log.duplicated().sum())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:17:14.680484500Z",
     "start_time": "2024-09-18T11:16:08.623656700Z"
    }
   },
   "id": "b05a9703db44e02a",
   "execution_count": 35
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "user_log.drop_duplicates(inplace=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:18:12.015875600Z",
     "start_time": "2024-09-18T11:17:14.686488Z"
    }
   },
   "id": "ea2c96927e2f314b",
   "execution_count": 36
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label\n0    34176         3906      0\n1    34176          121      0\n2    34176         4356      1\n3    34176         2217      0\n4   230784         4818      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv('train_format1.csv')\n",
    "train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:18:12.387506900Z",
     "start_time": "2024-09-18T11:18:12.007870300Z"
    }
   },
   "id": "75111d5dd5f7e736",
   "execution_count": 37
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label  age_range  gender\n0    34176         3906      0        6.0     0.0\n1    34176          121      0        6.0     0.0\n2    34176         4356      1        6.0     0.0\n3    34176         2217      0        6.0     0.0\n4   230784         4818      0        0.0     0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = pd.merge(train,user_info, on='user_id')\n",
    "df_train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:18:13.188037400Z",
     "start_time": "2024-09-18T11:18:12.359489500Z"
    }
   },
   "id": "216f675c529a3679",
   "execution_count": 38
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label  age_range  gender  item_id  cat_id  brand_id  \\\n0    34176         3906      0        6.0     0.0   757713     821    6268.0   \n1    34176         3906      0        6.0     0.0   718096    1142    6268.0   \n2    34176         3906      0        6.0     0.0   757713     821    6268.0   \n3    34176         3906      0        6.0     0.0   613698     821    6268.0   \n4    34176         3906      0        6.0     0.0   757713     821    6268.0   \n\n   time_stamp  action_type  \n0        1110            0  \n1        1031            3  \n2        1031            3  \n3        1021            0  \n4        1108            0  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>item_id</th>\n      <th>cat_id</th>\n      <th>brand_id</th>\n      <th>time_stamp</th>\n      <th>action_type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>757713</td>\n      <td>821</td>\n      <td>6268.0</td>\n      <td>1110</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>718096</td>\n      <td>1142</td>\n      <td>6268.0</td>\n      <td>1031</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>757713</td>\n      <td>821</td>\n      <td>6268.0</td>\n      <td>1031</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>613698</td>\n      <td>821</td>\n      <td>6268.0</td>\n      <td>1021</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>757713</td>\n      <td>821</td>\n      <td>6268.0</td>\n      <td>1108</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.rename(columns={'seller_id':'merchant_id'},inplace=True)\n",
    "df_train = pd.merge(df_train,user_log,on=['user_id','merchant_id'],how='left')\n",
    "df_train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:18:28.956488300Z",
     "start_time": "2024-09-18T11:18:12.943877200Z"
    }
   },
   "id": "f55104e9f1c1a701",
   "execution_count": 39
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  prob  age_range  gender  item_id  cat_id  brand_id  \\\n0   163968         4605   NaN        0.0     0.0   772645    1368    7622.0   \n1   163968         4605   NaN        0.0     0.0   772645    1368    7622.0   \n2   360576         1581   NaN        2.0     2.0   948181     614    4066.0   \n3   360576         1581   NaN        2.0     2.0  1111020     614    4066.0   \n4   360576         1581   NaN        2.0     2.0   294442     614    4066.0   \n\n   time_stamp  action_type  \n0        1111            2  \n1        1111            0  \n2        1111            2  \n3        1111            2  \n4        1111            2  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>prob</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>item_id</th>\n      <th>cat_id</th>\n      <th>brand_id</th>\n      <th>time_stamp</th>\n      <th>action_type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>163968</td>\n      <td>4605</td>\n      <td>NaN</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>772645</td>\n      <td>1368</td>\n      <td>7622.0</td>\n      <td>1111</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>163968</td>\n      <td>4605</td>\n      <td>NaN</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>772645</td>\n      <td>1368</td>\n      <td>7622.0</td>\n      <td>1111</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>360576</td>\n      <td>1581</td>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>948181</td>\n      <td>614</td>\n      <td>4066.0</td>\n      <td>1111</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>360576</td>\n      <td>1581</td>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>1111020</td>\n      <td>614</td>\n      <td>4066.0</td>\n      <td>1111</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>360576</td>\n      <td>1581</td>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>294442</td>\n      <td>614</td>\n      <td>4066.0</td>\n      <td>1111</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = pd.read_csv('test_format1.csv')\n",
    "df_test = pd.merge(test,user_info, on='user_id')\n",
    "df_test = pd.merge(df_test,user_log,on=['user_id','merchant_id'],how='left')\n",
    "df_test.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:24:01.525102600Z",
     "start_time": "2024-09-18T11:23:39.204456600Z"
    }
   },
   "id": "5a27192fcc08ec0a",
   "execution_count": 40
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "         user_id  merchant_id  age_range  gender  item_id  cat_id  brand_id  \\\n231886    348900          152        0.0     0.0   933865    1553    2773.0   \n291970    173310         1565        6.0     0.0   558566    1238     560.0   \n373598    161055         3349        3.0     0.0   801716     662    3358.0   \n493518    256851         4282        6.0     1.0   576436     252    7892.0   \n250767    373485         2954        4.0     1.0   162559     469    1847.0   \n...          ...          ...        ...     ...      ...     ...       ...   \n259178    256752         3986        6.0     1.0    20726    1189    3537.0   \n1414414    93410         4173        3.0     0.0   341586     389    5376.0   \n131932    111033         1963        4.0     1.0   887836    1023    6109.0   \n671155    401056         4282        5.0     1.0   684029     178    7892.0   \n121958    165045          148        0.0     0.0  1018583     326    7092.0   \n\n         time_stamp  action_type  \n231886         1111            0  \n291970         1110            0  \n373598         1111            0  \n493518         1111            0  \n250767         1111            0  \n...             ...          ...  \n259178         1111            0  \n1414414        1110            0  \n131932         1110            0  \n671155         1026            0  \n121958         1109            0  \n\n[533534 rows x 9 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>item_id</th>\n      <th>cat_id</th>\n      <th>brand_id</th>\n      <th>time_stamp</th>\n      <th>action_type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>231886</th>\n      <td>348900</td>\n      <td>152</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>933865</td>\n      <td>1553</td>\n      <td>2773.0</td>\n      <td>1111</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>291970</th>\n      <td>173310</td>\n      <td>1565</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>558566</td>\n      <td>1238</td>\n      <td>560.0</td>\n      <td>1110</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>373598</th>\n      <td>161055</td>\n      <td>3349</td>\n      <td>3.0</td>\n      <td>0.0</td>\n      <td>801716</td>\n      <td>662</td>\n      <td>3358.0</td>\n      <td>1111</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>493518</th>\n      <td>256851</td>\n      <td>4282</td>\n      <td>6.0</td>\n      <td>1.0</td>\n      <td>576436</td>\n      <td>252</td>\n      <td>7892.0</td>\n      <td>1111</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>250767</th>\n      <td>373485</td>\n      <td>2954</td>\n      <td>4.0</td>\n      <td>1.0</td>\n      <td>162559</td>\n      <td>469</td>\n      <td>1847.0</td>\n      <td>1111</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>259178</th>\n      <td>256752</td>\n      <td>3986</td>\n      <td>6.0</td>\n      <td>1.0</td>\n      <td>20726</td>\n      <td>1189</td>\n      <td>3537.0</td>\n      <td>1111</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1414414</th>\n      <td>93410</td>\n      <td>4173</td>\n      <td>3.0</td>\n      <td>0.0</td>\n      <td>341586</td>\n      <td>389</td>\n      <td>5376.0</td>\n      <td>1110</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>131932</th>\n      <td>111033</td>\n      <td>1963</td>\n      <td>4.0</td>\n      <td>1.0</td>\n      <td>887836</td>\n      <td>1023</td>\n      <td>6109.0</td>\n      <td>1110</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>671155</th>\n      <td>401056</td>\n      <td>4282</td>\n      <td>5.0</td>\n      <td>1.0</td>\n      <td>684029</td>\n      <td>178</td>\n      <td>7892.0</td>\n      <td>1026</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>121958</th>\n      <td>165045</td>\n      <td>148</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1018583</td>\n      <td>326</td>\n      <td>7092.0</td>\n      <td>1109</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>533534 rows × 9 columns</p>\n</div>"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X = df_train.drop('label',axis=1)\n",
    "y = df_train['label']\n",
    "X_train,X_val,y_train,y_val = train_test_split(X, y, test_size=0.7, random_state=42)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T11:29:40.305456400Z",
     "start_time": "2024-09-18T11:29:14.994972600Z"
    }
   },
   "id": "231aba80b2fab444",
   "execution_count": 41
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\pycharm111\\Anaconda3\\Lib\\site-packages\\sklearn\\metrics\\_classification.py:1509: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
      "  _warn_prf(average, modifier, f\"{metric.capitalize()} is\", len(result))\n",
      "F:\\pycharm111\\Anaconda3\\Lib\\site-packages\\sklearn\\metrics\\_classification.py:1509: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
      "  _warn_prf(average, modifier, f\"{metric.capitalize()} is\", len(result))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型的评估报告：\n",
      "               precision    recall  f1-score   support\n",
      "\n",
      "           0       0.90      1.00      0.95   1120013\n",
      "           1       0.00      0.00      0.00    124900\n",
      "\n",
      "    accuracy                           0.90   1244913\n",
      "   macro avg       0.45      0.50      0.47   1244913\n",
      "weighted avg       0.81      0.90      0.85   1244913\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\pycharm111\\Anaconda3\\Lib\\site-packages\\sklearn\\metrics\\_classification.py:1509: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
      "  _warn_prf(average, modifier, f\"{metric.capitalize()} is\", len(result))\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import classification_report\n",
    "\n",
    "model = RandomForestClassifier(max_depth=2, random_state=0)\n",
    "model.fit(X_train,y_train)\n",
    "y_pred=model.predict(X_val)\n",
    "y_proba = model.predict_proba(X_val)\n",
    "print('模型的评估报告：\\n',classification_report(y_val, y_pred))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T12:22:37.490130700Z",
     "start_time": "2024-09-18T12:21:33.540833300Z"
    }
   },
   "id": "a1d1ebf5db12cef1",
   "execution_count": 48
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "0.5"
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import roc_auc_score\n",
    "auc=roc_auc_score(y_val,y_pred)\n",
    "auc"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T13:11:28.475979300Z",
     "start_time": "2024-09-18T13:11:26.677787200Z"
    }
   },
   "id": "1e6306d7c8941307",
   "execution_count": 61
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型的评估报告：\n",
      "               precision    recall  f1-score   support\n",
      "\n",
      "           0       0.93      0.79      0.85   1120013\n",
      "           1       0.21      0.50      0.29    124900\n",
      "\n",
      "    accuracy                           0.76   1244913\n",
      "   macro avg       0.57      0.64      0.57   1244913\n",
      "weighted avg       0.86      0.76      0.80   1244913\n"
     ]
    }
   ],
   "source": [
    "model1 = RandomForestClassifier(max_depth=10, random_state=0,class_weight='balanced')\n",
    "model1.fit(X_train,y_train)\n",
    "y_pred1=model1.predict(X_val)\n",
    "y_proba1 = model1.predict_proba(X_val)\n",
    "print('模型的评估报告：\\n',classification_report(y_val, y_pred1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T12:35:56.994368600Z",
     "start_time": "2024-09-18T12:32:22.468044300Z"
    }
   },
   "id": "d678f0a05dc415b3",
   "execution_count": 53
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "0.6407718478765198"
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auc1 = roc_auc_score(y_val,y_pred1)\n",
    "auc1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T13:12:21.210390700Z",
     "start_time": "2024-09-18T13:12:19.091988300Z"
    }
   },
   "id": "afc91b7279bb790f",
   "execution_count": 62
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 0, 0, ..., 0, 0, 0], dtype=int64)"
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test = df_test.drop('prob',axis=1)\n",
    "\n",
    "y_predict = model1.predict(X_test)\n",
    "y_predict"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-18T13:24:02.250494600Z",
     "start_time": "2024-09-18T13:23:44.213953600Z"
    }
   },
   "id": "2413f997a26ec5ba",
   "execution_count": 63
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
